package com.myblog.handler.exception;

import com.myblog.domain.GlobalResponse;
import com.myblog.domain.entity.enums.GlobalResponseEnum;
import com.myblog.expection.GlobalException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(GlobalException.class)
    public GlobalResponse globalException(GlobalException e){
        //打印异常信息
        log.error("出现了异常!",e);
        //从异常对象中获取信息返回
        return GlobalResponse.error(e.getCode(),e.getMsg());
    }

    @ExceptionHandler(Exception.class)
    public GlobalResponse Exception(Exception e){
        //打印异常信息
        log.error("出现了异常!",e);
        //从异常对象中获取信息返回
        return GlobalResponse.error(GlobalResponseEnum.ERROR.getCode(),e.getMessage());
    }

}
